package com.xieyong.mywebsite.controller;

import com.github.pagehelper.PageInfo;
import com.xieyong.mywebsite.model.dto.Result;
import com.xieyong.mywebsite.model.entity.Notice;
import com.xieyong.mywebsite.service.Impl.BaseNoticeServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/notice")
public class BaseNoticeController {

    @Resource
    private BaseNoticeServiceImpl baseNoticeServiceImpl;

    @GetMapping("/{id}")
    public Result<Notice> getById(@PathVariable Integer id) {
        Notice notice = baseNoticeServiceImpl.getById(id);
        return Result.success(notice);
    }

    @GetMapping("/page")
    public Result<PageInfo<Notice>> getPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime,
            Notice notice) {
        PageInfo<Notice> noticePageInfo = baseNoticeServiceImpl.getPage(pageNum, pageSize, notice,startTime,endTime);
        return Result.success(noticePageInfo);
    }

    @GetMapping("/all")
    public Result<List<Notice>> getAll() {
        return Result.success(baseNoticeServiceImpl.getAll());
    }

    @PostMapping()
    public Result<String> add(@RequestBody Notice notice) {
        baseNoticeServiceImpl.add(notice);
        return Result.success();
    }

    @PutMapping()
    public Result<String> update(@RequestBody Notice notice) {
        baseNoticeServiceImpl.update(notice);
        return Result.success();
    }

    @DeleteMapping()
    public Result<String> delete(@RequestParam Integer id) {
        baseNoticeServiceImpl.delete(id);
        return Result.success();
    }
    @DeleteMapping("/batch")
    public Result<String> deleteBatch(@RequestBody List<Integer> ids) {
        baseNoticeServiceImpl.deleteBatch(ids);
        return Result.success();
    }


}
